<?php

/**
 * 你是我的小苹果—苹果明星大评选
 * 2014-07
 */
class MainModel extends Model{

    public static $__DB;

    /**
     * 构造函数
     * @return 无
     */
    function __construct(){
        parent::__construct();
        self::$__DB = new DB();
        self::$__DB->load_dsn('act_2016');
    }

    /**
     * 析构函数
     * @return 无
     */
    function __destruct(){
        self::$__DB->close();
    }

    /**
     * 得到分页列表
     *
     * @param $table
     * @param $where
     * @param $page
     */
    function getPageList($where, $page, $pageSize = 10){
        $sql   = "select count(*) as num from (select uin,zone_id from axwxd_log {$where} group by uin,zone_id) tmp";
        $total = self::$__DB->get_value($sql);
        $sql   = "select zone_id,role_name,sum(vote) apple_vote from (select * from axwxd_log {$where} order by log_time desc) tmp group by uin,zone_id order by apple_vote desc";
        $ret   = self::$__DB->query_with_page($sql, $page, $total, $pageSize);
        if($total > REVERSE_NUM && ($page > $total / 2)){
            $new_rows = array();
            $rows     = $ret['rows'];
            $count    = count($rows);
            for($i = $count - 1; $i >= 0; $i--){
                $new_rows[] = $rows[$i];
            }
            $ret['rows'] = $new_rows;
        }
        return $ret;
    }

    /**
     * @param $isAsset   是否是资产
     * @param $UIN
     * @param $ZoneID
     * @param $itemID    物品ID(16进制)
     * @param $useMeans  使用方式1计时，2计数
     * @param $itemNum   时效/个数
     * @param $itemName  物品名称
     * @param $actName   活动名称
     * @param $channelID 渠道
     */
    function sendGameItem1($isAsset = 0, $UIN, $ZoneID, $itemID, $useMeans, $itemNum, $itemName = '', $actName = '新炫舞官方活动', $channelID = 0x01020008, $op = 1){
        if(empty($UIN) || empty($ZoneID) || empty($itemID)){
            return 0;
        }
        load_extend('PromoterBill');
        $gm_server = new gm_server();
        if($isAsset == 1){
            $email['is_mail'] = null;
            $email['sender']  = null;
            $email['content'] = null;
            if($itemID == '0x10'){
                $response = $gm_server->send51Point($UIN, 1, $ZoneID, 'admin', 1, 0x10, $itemNum, $email, $actName, 111, $channelID); //代币
            }else{
                if($itemID == '0x08'){
                    $response = $gm_server->send51Point($UIN, 1, $ZoneID, 'admin', 1, 0x08, $itemNum, $email, $actName, 111, $channelID); //游戏币
                }else{
                    if($itemID == '0x800'){
                        $response = $gm_server->send51Point($UIN, 1, $ZoneID, 'admin', 1, 0x800, $itemNum, $email, $actName, 111, $channelID); //
                    }else{
                        if($itemID == '0x04'){
                            $response = $gm_server->send51Point($UIN, 1, $ZoneID, 'admin', 1, 0x04, $itemNum, $email, $actName, 111, $channelID); //
                        }
                    }
                }
            }
            unset($email);
        }else{
            $ItemUnit           = new ItemUnit();
            $ItemUnit->itemID   = hexdec($itemID); //十六进制转为十进制
            $ItemUnit->useMeans = $useMeans;

            if($ItemUnit->useMeans == 1){
                $ItemUnit->nQuantity = $itemNum == -1 ? 2147483647 : $itemNum; //2147483647-表示永久
            }else{
                $ItemUnit->nQuantity = $itemNum;
            }
            $ItemUnit->nTimeLimit = 2147483647; // 固定值
            $arrItemUnit[]        = $ItemUnit;
            $response             = $gm_server->sendGoods($UIN, 1, $ZoneID, 'admin', $arrItemUnit, $actName . $itemName, $op);
            unset($ItemUnit, $arrItemUnit);
        }
        unset($gm_server);
        if($response && $response->nErrorCode == '0'){//成功
            return 1;
        }else{
            return isset($response) ? $response->nErrorCode : 0;
        }
    }

    function sendGameItem($UIN, $ZoneID, $itemID, $useMeans, $itemNum, $itemName = '', $actName = '新炫舞官方活动', $channelID = 0x01020008){
        if(empty($UIN) || empty($ZoneID) || empty($itemID)){
            return 0;
        }
        load_extend('PromoterBill');
        $gm_server = new gm_server();

        $ItemUnit           = new ItemUnit();
        $ItemUnit->itemID   = hexdec($itemID); //十六进制转为十进制
        $ItemUnit->useMeans = $useMeans;

        if($ItemUnit->useMeans == 1){
            $ItemUnit->nQuantity = $itemNum == -1 ? 2147483647 : $itemNum; //2147483647-表示永久
        }else{
            $ItemUnit->nQuantity = $itemNum;
        }
        $ItemUnit->nTimeLimit = 2147483647; // 固定值
        $arrItemUnit[]        = $ItemUnit;
        $response             = $gm_server->sendGoods($UIN, 1, $ZoneID, 'admin', $arrItemUnit, $actName . $itemName);
        unset($ItemUnit, $arrItemUnit);

        unset($gm_server);
        if($response && $response->nErrorCode == '0'){//成功
            return 1;
        }else{
            return isset($response) ? $response->nErrorCode : 0;
        }
    }

    /*
     * 获取列表
     */
    function get_list($tb, $where = '', $order = '', $limit = '', $db = ''){
        if(empty($tb)){
            return false;
        }
        if(!empty($where)){
            $where = ' WHERE ' . $where;
        }
        $sql = "SELECT * FROM `{$tb}` {$where} {$order} {$limit}";
        //echo $sql;
        return self::$__DB->get_rows($sql);
    }

    /*
     * 获取部分属性
     */
    function get_item($tb, $item = '*', $where = '', $order = '', $limit = '', $db = ''){
        if(empty($tb)){
            return false;
        }
        if(!empty($where)){
            $where = ' WHERE ' . $where;
        }
        $sql = "SELECT {$item} FROM `{$tb}` {$where} {$order} {$limit}";
        return self::$__DB->get_rows($sql);
    }

    //帮助——给我投票前10
    function get_help($uin, $type){
        if(empty($uin) || empty($type)){
            exit();
        }
        if($type == 1){
            //游戏币投票前10
            $sql = <<<EOT
select zone_id,role_name,sum(vote) vote from
(select * from bbpx2016_log where f_uin={$uin} and status=1 order by log_time desc) tmp
		group by uin,zone_id order by vote desc limit 10
EOT;
        }else{
            //type==2，送玫瑰投票前10
            $sql = <<<EOT
select zone_id,role_name,sum(vote) apple_vote from
(select * from bbpx2016_log where f_uin={$uin} and status=1 and money_id>0 order by log_time desc) tmp
		group by uin,zone_id order by apple_vote desc limit 10
EOT;
        }

        return self::$__DB->get_rows($sql);
    }

    /*
     * 获取总记录数
     */
    function get_count($tb, $where = ''){
        if(empty($tb)){
            return false;
        }
        if(!empty($where)){
            $where = ' WHERE ' . $where;
        }
        $sql = "SELECT COUNT(*) FROM `{$tb}` {$where}";
        //echo $sql;
        return self::$__DB->get_value($sql);
    }

    /*
     * 插入记录
     */
    function insert_data($tb, $record){
        if(empty($tb) || !is_array($record) || empty($record)){
            return false;
        }
        $sql = "INSERT INTO `{$tb}` SET " . sql_implode($record);

        self::$__DB->query($sql);
        return self::$__DB->insert_id();
    }
    /**
     * 插入数据
     *
     * @param $param
     * @param $table
     * @param $dsn
     */
    function insert_datas($table, $param, $return = TRUE, $DNS = 'act_2016'){
        if(!is_array($param) || empty($param) || empty($table)){
            return false;
        }
        self::$__DB->load_dsn($DNS);
        $param         = self::$__DB->escape_parameter($param);
        $insert_string = self::$__DB->array_to_string($param, 'insert');
        $sql           = " insert  into `{$table}`   {$insert_string}  ";
        $this->tempSql = $sql;
        self::$__DB->query($sql);
        unset($param);
        if($return){
            return self::$__DB->insert_id();
        }else{
            return true;
        }
    }
    /*
     * 更新记录
     */
    function update_data($tb, $record, $where = '', $other = '', $db = ''){
        if(empty($tb) || !is_array($record) || (empty($record) && empty($other))){
            return false;
        }
        if(!empty($where)){
            $where = ' WHERE ' . $where;
        }
        if(!empty($record) && !empty($other)){
            $other = ',' . $other;
        }
        $sql = "UPDATE `{$tb}` SET " . sql_implode($record) . $other . $where;
        //echo $sql;
        return self::$__DB->query($sql);
    }

    /*
     * 获取分页列表
     */
    function get_list_page($tb, $where = '', $current_page = 1, $page_size = DB_DEFAULT_PAGE_SIZE, $order = 'ORDER BY id DESC', $total_rows = 0){
        if(empty($tb)){
            return false;
        }
        if(!empty($where)){
            $where = ' WHERE ' . $where;
        }
        $sql = "SELECT * FROM `{$tb}` {$where} {$order}";
        //echo $sql;
        return self::$__DB->query_with_page($sql, $current_page, $total_rows, $page_size);
    }

    /**
     * 扣除玩家51点
     *
     * @param $UIN
     * @param $ZoneID
     * @param $Money
     * @param $ChannelID
     */
    function modifyUserMoney($UIN, $ZoneID, $Money, $ChannelID = 0x09020030, &$OldAsset = 0){
        if(empty($UIN) || empty($ZoneID) || empty($Money) || empty($ChannelID)){
            return 0;
        }
        load_extend('PromoterBill');
        $gm_server        = new gm_server();
        $email            = array();
        $email['is_mail'] = 0; //0-不发，1-发送
        $email['sender']  = '';
        $email['content'] = '';
        $response         = $gm_server->send51Point($UIN, 1, $ZoneID, 'admin', 1, 0x20, 0 - $Money, $email, '男神女神', 111, $ChannelID);
        if($response){
            if($response->nErrorCode == 0){
                $code     = 1; //成功
                $OldAsset = $response->arrOldAsset;
            }elseif($response->nErrorCode == -2063269883 || $response->nErrorCode == 2231697413){ //51点不足(-2063269883|FFFFFFFF85050005,2231697413|85050005)
                $code = -1; //51点不足
            }else{
                $code = $response->nErrorCode; //其他错误
            }
        }else{
            $code = 0; //没有响应
        }
        unset($email, $gm_server);
        return $code;
    }

    //游戏玩家信息
    function getGameUserInfo($UIN, $ZoneID){
        return $retsult = getGameUserInfoByUin($UIN, $ZoneID);
    }
    /**
     * 一条
     *
     * @param $table
     * @param $where
     * @param $field
     */
    function getRow($table, $where = '', $field = '*', $DNS = 'act_2016'){
        if(empty($table)){
            exit('NOT Table');
        }
        $sql           = "select {$field} from {$table} {$where}";
        $this->tempSql = $sql;
        self::$__DB->load_dsn($DNS);
        return self::$__DB->get_row($sql);
    }

    /**
     * 多条
     *
     * @param $table
     * @param $where
     * @param $field
     */
    function getRows($table, $where = '', $field = '*', $DNS = 'act_2016'){
        if(empty($table)){
            exit('NOT Table');
        }

        $sql           = "select {$field} from {$table} {$where}";
        $this->tempSql = $sql;
        self::$__DB->load_dsn($DNS);
        return self::$__DB->get_rows($sql);
    }
    /**
     * 更新数据
     *
     * @param $table
     * @param $param
     * @param $where
     * @param $DNS
     */
    function update_datas($table, $param, $where = '', $DNS = 'act_2016'){
        if(empty($table) || empty($param) || !is_array($param)){
            return false;
        }
        self::$__DB->load_dsn($DNS);
        $param         = self::$__DB->escape_parameter($param);
        $field         = self::$__DB->array_to_string($param, 'update');
        $sql           = "UPDATE `{$table}` SET {$field} {$where}";
        $this->tempSql = $sql;
        unset($param);
        return self::$__DB->query($sql);
    }
    /**
     * 读帐单
     * Enter description here ...
     *
     * @param  $t
     */
    function getBillValue($date, $roleID, $zoneID, $t, $retType = 'value', $DNS = 'bill',$t1,$t2){
        self::$__DB->load_dsn($DNS);
        switch($t){
            case 1://在线时间
                $sql = "select TotalTime `total` from dmz_bill_{$date}.mb_onlinetime where ZoneID='{$zoneID}' and RoleID='{$roleID}' order by BillTime  desc    limit 1";
                break;
            case 2://个人争霸胜利
                $sql = "select count(*) `count` from dmz_bill_{$date}.gameresult where ZoneID='{$zoneID}' and RoleID='{$roleID}' ";
                break;
            case 3://翻翻乐
                $sql = "select count(*) `count` from dmz_bill_{$date}.lotterybet where ZoneID='{$zoneID}' and RoleID='{$roleID}'";
                break;
            case 4://家族贡献点
                $sql = "select sum(DevoteUpdated) `total` from dmz_bill_{$date}.member_devote where ZoneID='{$zoneID}' and RoleID='{$roleID}' and Source=6  ";
                break;
            case 5://成功挑战星座守护战任意关卡次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.attackstarcity where ZoneID='{$zoneID}' and RoleID='{$roleID}' and CityID between 0x00010000 and 0x000c000f and MissionState=2";
                break;
            case 6://成功挑战梦幻十二宫关卡次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.attackstarcity where ZoneID='{$zoneID}' and RoleID='{$roleID}' and CityID between 0x10010000 and 0x100cFFFF and MissionState=2";
                break;
            case 7://成功祈福次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.pray where ZoneID='{$zoneID}' and RoleID='{$roleID}' and ErrorCode=0 and ResultID=1";
                break;
            case 8://晶钻祈福次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.pray where ZoneID='{$zoneID}' and RoleID='{$roleID}' and PrayType=1 and ErrorCode=0";
                break;
            case 9://被赠送鲜花次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.sendflower where ZoneID='{$zoneID}' and ReceiverID='{$roleID}'";
                break;
            case 10://太鼓玩法对局次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.balance where ZoneID='{$zoneID}' and RoleID='{$roleID}' and GamePlay=3";
                break;
            case 11://完成随机任务个数       //0x02000000
                $sql = "select count(*) `count` from dmz_bill_{$date}.tasknewinfo where ZoneID='{$zoneID}' and RoleID='{$roleID}' and TaskID between 0x03000000 and  0x03ffffff and result = 0 and `type` = 3 ";
                break;
            case 12://进击的piku对局次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.balance where ZoneID='{$zoneID}' and RoleID='{$roleID}' and GameMode =21 ";
                break;
            case 13://对局获得晶钻个数
                $sql = "select sum(Quantity) total from dmz_bill_{$date}.raffle_getfragmentresponse where ZoneID='{$zoneID}' and RoleID='{$roleID}' and ErrorCode=0";
                break;
            case 14://赠送鲜花次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.sendflower where ZoneID='{$zoneID}' and SenderID='{$roleID}'";
                break;
            case 15://幸运模式对局次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.balance where ZoneID='{$zoneID}' and RoleID='{$roleID}' and GameMode =7 ";
                break;
            case 16://团队PK对局次数
                $sql = "select count(*) `count` from dmz_bill_{$date}.balance where ZoneID='{$zoneID}' and RoleID='{$roleID}' and GameMode =4 ";
                break;
            case 17://成功挑战蛇岛任意关卡
                $sql = "select count(*) `count` from dmz_bill_{$date}.attackstarcity where ZoneID='{$zoneID}' and RoleID='{$roleID}' and CityID between 0x000d0000 and 0x000d000f";
                break;
            case 18://合成工坊合成晶钻道具≥3次
                $sql = "select count(*) `count` from dmz_bill_{$date}.composite_success where ZoneID='{$zoneID}' and RoleID='{$roleID}' ";
                break;
            case 19://进行2人以上游戏且得分大于8000
                $sql = "select count(*) `count` from dmz_bill_{$date}.balance where ZoneID='{$zoneID}' and RoleID='{$roleID}' and PlayerCount>=2 and Score>=8000";
                break;
            case 20://进行4人以上游戏且得分大于5000
                $sql = "select count(*) `count` from dmz_bill_{$date}.balance where ZoneID='{$zoneID}' and RoleID='{$roleID}' and PlayerCount>=4 and Score>=5000";
                break;
            case 21://用户登录
                $sql = "select count(*) `count` from dmz_bill_{$date}.login where ZoneID='{$zoneID}' and RoleID='{$roleID}'";
                break;
            case 22: //游戏充值
                $sql = "select sum(MAmount) `total` from dmz_bill_{$date}.pay where ZoneID ='{$zoneID}' and PayRoleID ='{$roleID}'";
                break;
            case 23://每条记录300s(记录数)
                $sql = "select count(*) from dmz_bill_{$date}.mb_onlinetime where ZoneID='{$zoneID}' and RoleID='{$roleID}'";
                break;
            case 24://完成日常任务个数       //0x02000000
                $sql = "select count(*) `count` from dmz_bill_{$date}.tasknewinfo where ZoneID='{$zoneID}' and RoleID='{$roleID}' and TaskID between 0x02000000 and  0x02ffffff and result = 0 and `type` = 3 ";
                break;
            case 25:  //大喇叭个数
                $sql = "select sum(moneyold-moneynew)/50 from dmz_bill_{$date}.money where Channelid = 0x08020001 and RoleID='{$roleID}' and ZoneID='{$zoneID}' and Billtime>='{$t1}' and Billtime<='{$t2}'";
                break;
            case 26:   //魅力值
                $sql = "select sum(ValueReal) from dmz_bill_{$date}.charm  where valuereal > 0 and RoleID='{$roleID}' and ZoneID='{$zoneID}' and Billtime>='{$t1}' and Billtime<='{$t2}'";
                break;
            default:
                $sql = '';
        }
        if(empty($sql)){
            return false;
        }
        $this->tempSql = $sql;
        if(strtolower($retType) == 'value'){
            return self::$__DB->get_value($sql);
        }else{
            return self::$__DB->get_row($sql);
        }
    }

}

?>
